package dao;

import entity.User;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDaoImpl implements IUserDao {

    private Connection conn;
    private Statement stmt;
    private PreparedStatement ps;
    private ResultSet rs;

    @Override
    public User selectStudentLogin(String username, String password) {
        User user = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select * from user where userName=? and userPassword=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();

            if(rs.next()){
                user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("userpassword"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn, stmt, rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return user;
    }

}
